#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>

class Client
{
public:
    Client(std::string& IP, uint16_t port)
    :IP_(IP), port_(port)
    {
        socket_ = socket(AF_INET, SOCK_DGRAM, 0);
        if (socket_ == -1)
        {
            std::cerr << "socket error :" << errno << strerror(errno) << std::endl;
            exit(1);
        }
        serverAddr.sin_family = AF_INET;
        serverAddr.sin_addr.s_addr = inet_addr(IP_.c_str());
        serverAddr.sin_port = htons(port_);
    }

    void send(void)
    {
        char buff[1024];
        socklen_t to_len = sizeof(serverAddr);
        while(true)
        {
            std::cout << "please say: ";
            std::cin >> buff; 
            sendto(socket_, buff, sizeof(buff), 0, (sockaddr*)&serverAddr, to_len);
        }
    }

    ~Client()
    {
        close(socket_);
    }
private:
    int socket_;
    std::string IP_;
    uint16_t port_;
    struct sockaddr_in serverAddr;
};